package com.cskaoyan.shopping.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.api.CartsForOrder;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.UserInfo;
import com.cskaoyan.shopping.service.IShoppingCartService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 1784549954@qq.com
 * @since 2022/12/03 10:21
 */
@Service
public class IShoppingCartServiceImpl implements IShoppingCartService {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    CartItemConverter cartItemConverter;

    @Override
    public void postCarts(CartRequest cartRequest) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(cartRequest.getUserId().toString());
        Item item = itemMapper.selectByPrimaryKey(cartRequest.getProductId());
        CartProductDto cartProductDto = cartItemConverter.item2Dto(item);
        cartProductDto.setChecked("false");
        cartProductDto.setProductNum(Long.valueOf(cartRequest.getProductNum()));
        map.put(cartRequest.getProductId(),cartProductDto);
    }

    @Override
    public List<CartProductDto> getCsrts(HttpServletRequest request) {
        String userInfo = request.getHeader("user_info");
        UserInfo userInfo1 = JSON.parseObject(userInfo, UserInfo.class);
        RMap<Long, CartProductDto> map = redissonClient.getMap(userInfo1.getUid().toString());
        List<CartProductDto> cartProductDtos = new ArrayList<>();
        Set<Long> longs = map.keySet();
        for (Long aLong : longs) {
            cartProductDtos.add(map.get(aLong));
        }
        return cartProductDtos;
    }

    @Override
    public void putCarts(CartRequest cartRequest) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(cartRequest.getUserId().toString());
        CartProductDto cartProductDto = map.get(cartRequest.getProductId());
        cartProductDto.setProductNum(Long.valueOf(cartRequest.getProductNum()));
        cartProductDto.setChecked(cartRequest.getChecked());
        map.put(cartRequest.getProductId(),cartProductDto);
    }

    @Override
    public void cartsForOrder(CartsForOrder cartsForOrder) {
        RMap<Long, CartProductDto> map = redissonClient.getMap(cartsForOrder.getUserId());
        List<Long> deleteGoodsProductIds = cartsForOrder.getDeleteGoodsProductIds();
        for (Long deleteGoodsProductId : deleteGoodsProductIds) {
            map.remove(deleteGoodsProductId);
        }
    }
}
